layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 库存变更记录管理
     */
    var StockLog = {
        tableId: "stockLogTable"
    };

    /**
     * 初始化表格的列
     */
    StockLog.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'productId', sort: true, title: '商品'},
            {field: 'supplierId', sort: true, title: '供应商'},
            {field: 'shopId', sort: true, title: '分店'},
            {field: 'warehouseId', sort: true, title: '仓库'},
            {field: 'shelfId', sort: true, title: '货架'},
            {field: 'operateType', sort: true, title: '库存操作类型'},
            {field: 'orderType', sort: true, title: '关联单据类型'},
            {field: 'orderId', sort: true, title: '关联单据ID'},
            {field: 'saleUsableCount', sort: true, title: '销售可用库存'},
            {field: 'saleFreezeCount', sort: true, title: '销售冻结库存'},
            {field: 'saleUsedCount', sort: true, title: '销售已用库存'},
            {field: 'saleUsableChange', sort: true, title: '销售可用库存变更'},
            {field: 'saleFreezeChange', sort: true, title: '销售冻结库存变更'},
            {field: 'saleUsedChange', sort: true, title: '销售已用库存变更'},
            {field: 'warehouseUsableCount', sort: true, title: '仓库可用库存'},
            {field: 'warehouseFreezeCount', sort: true, title: '仓库冻结库存'},
            {field: 'warehouseUsedCount', sort: true, title: '仓库已用库存'},
            {field: 'warehouseUsableChange', sort: true, title: '仓库可用库存变更'},
            {field: 'warehouseFreezeChange', sort: true, title: '仓库冻结库存变更'},
            {field: 'warehouseUsedChange', sort: true, title: '仓库冻结库存变更'},
            {field: 'version', sort: true, title: '乐观锁'},
            {field: 'createTime', sort: true, title: '创建时间'},
            {field: 'updateTime', sort: true, title: '更新时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    StockLog.search = function () {
        var queryData = {};

        queryData['productId'] = $('#productId').val();
        queryData['supplierId'] = $('#supplierId').val();
        queryData['shopId'] = $('#shopId').val();
        queryData['warehouseId'] = $('#warehouseId').val();
        queryData['shelfId'] = $('#shelfId').val();

        table.reload(StockLog.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    StockLog.openAddDlg = function () {
        func.open({
            title: '添加库存变更记录',
            content: Feng.ctxPath + '/stockLog/add',
            tableId: StockLog.tableId
        });
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      StockLog.openEditDlg = function (data) {
          func.open({
              title: '修改库存变更记录',
              content: Feng.ctxPath + '/stockLog/edit?id=' + data.id,
              tableId: StockLog.tableId
          });
      };


    /**
     * 导出excel按钮
     */
    StockLog.exportExcel = function () {
        var checkRows = table.checkStatus(StockLog.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    StockLog.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/stockLog/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(StockLog.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + StockLog.tableId,
        url: Feng.ctxPath + '/stockLog/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: StockLog.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        StockLog.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    StockLog.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        StockLog.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + StockLog.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            StockLog.openEditDlg(data);
        } else if (layEvent === 'delete') {
            StockLog.onDeleteItem(data);
        }
    });
});
